/**
 * 
 */
var rowNumber;
$(function(){
	   $('#activity_table')
		.datagrid(
				{
					url : 'admin/queryActivityInfo.do?pageSize=10',
					collapsible : true,
					width : '100%',
					height :'450px',
					iconCls : 'icon-user',
					fitColumns : true,
					pagination : true,
					rownumbers : true,
					singleSelect:true,
					checkOnSelect:false,
					selectOnCheck:false,
					loadMsg:'正在加载数据',
					loadFilter:function(data){
						for (var i = 0; i < data.rows.length; i++) {
							if (data.rows[i].projectStatus == 0) {
								data.rows[i].projectStatus = '未开始';
							}
							if (data.rows[i].projectStatus == 1) {
								data.rows[i].projectStatus = '进行中';
							}
						}
						 return data;
					},
					onClickRow: function(rowIndex, rowData) {
						rowNumber = rowIndex;
					},
					onDblClickRow :function(rowIndex,rowData){
						//layer.msg("double click");
					  },
					onLoadSuccess:function(data){
						$('#activity_table').datagrid('selectRow',rowNumber);
					},
					toolbar:'#toolbar',
					columns : [ [
							{
								field : 'projectTitle',
								title : '项目名称',
								width : 50,
								align : 'center'
							},
							{
								field : 'projectReadCount',
								title : '总阅读数量',
								width : 50,
								align : 'center'
							},
							{
								field : 'projectStatus',
								title : '项目状态',
								width : 50,
								align : 'center'
							},
							{
								field : 'projectReadMoney',
								title : '每次点击金额',
								width : 50,
								align : 'center'
							},
							{
								field : 'projectShareMoney',
								title : '项目分享的总金额',
								width : 50,
								align : 'center'
							},
							{
								field : 'projectShareUserLimit',
								title : '分享最高获得金额',
								width : 50,
								align : 'center'
							}
						   ] ]
				});
	 //设置分页控件  
		
	var p = $('#activity_table').datagrid('getPager');
		$(p).pagination({
			pageSize : 10,//每页显示的记录条数，默认为10  
			pageList : [ 5, 10, 20, 30, 40, 50 ],//可以设置每页记录条数的列表  
			beforePageText : '第',//页数文本框前显示的汉字  
			afterPageText : '页    共 {pages} 页',
			displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录',
			onSelectPage : function(pageNumber, pageSize) {
				//layer.msg("pageNumber="+pageNumber+"pageSize="+pageSize)
				var datas = {
					'projectTitle':$('#sprojectTitle').textbox('getValue'),
					currentPage : (pageNumber - 1) * pageSize,
					pageSize : pageSize
				}
				loadData(datas);
			},
			onRefresh : function(pageNumber, pageSize) {
				var datas = {
						'projectTitle':$('#sprojectTitle').textbox('getValue'),
						currentPage : (pageNumber - 1) * pageSize,
						pageSize : pageSize
					}
				loadData(datas);
			}
		});
	});
$(function(){
	$('#update').bind('click',function(){
		var row = $('#activity_table').datagrid('getSelected');
		if(!!row){
			$('#activityfm').form('clear');
			$('#activitydlg').dialog('open').dialog("setTitle","发布活动");
			$('#activityfm').form('load',row);
			$("#saveUrl").val('admin/updateActivityPublish.do?projectId='+row.projectId);
		}else{
			layer.msg('请选择需要参加活动的项目');
		}
	});
	$('#search').bind('click',function(){
		var pageSize=$('#activity_table').datagrid('getPager').data("pagination").options.pageSize;
		var datas = {
			'pageSize':pageSize,
			'projectTitle':$('#sprojectTitle').textbox('getValue'),
			'projectStatus':$('#sprojectStatus').textbox('getValue'),
			
		}
		loadData(datas);
	});
	$('#reset').bind('click',function(){
		$('#sprojectTitle').textbox('setValue','');
		$('#sprojectStatus').textbox('setValue','');
		var pageSize=$('#activity_table').datagrid('getPager').data("pagination").options.pageSize;
		var datas={
				'pageSize' : pageSize,
		}
		loadData(datas);
	});
	$('#save').bind('click',function(){
		var pageSize = $('#activity_table').datagrid('getPager').data("pagination").options.pageSize;
		var pageNumber = $('#activity_table').datagrid('getPager').data("pagination").options.pageNumber;
		var datas={
				currentPage : (pageNumber - 1) * pageSize,
				pageSize:pageSize
				};
		$.ajax({
			url : $("#saveUrl").val(),
			type : "POST",
			dataType : "json",
			data : {
				'projectReadMoney':$("#projectReadMoney").numberbox('getValue'),
				'projectShareMoney':$("#projectShareMoney").numberbox('getValue'),
				'projectShareUserLimit':$("#projectShareUserLimit").numberbox('getValue'),
			},
			beforeSend: function(){
				var flag=$("#activityfm").form('enableValidation').form('validate');
				if(flag){
				$('#save').linkbutton({disabled:true}); 
				 return flag;
				}else{
					return flag;
				}
		       
	         },
			success : function(result) {
				$('#save').linkbutton({disabled:false}); 
				if (result.result == 0) {
					layer.msg(result.tips);
				} else {
					layer.msg(result.tips);
					$('#activitydlg').dialog('close'); // close the dialog
					$('#activity_table').datagrid('reload',datas); // reload the data
				}
			}
		});
	});
	$('#cancel').bind('click',function(){
		$('#save').linkbutton({disabled:false}); 
		$('#activityfm').form('clear');
		$('#activitydlg').dialog('close');
	});
});
var loadData = function(datas){
	$.ajax({
		url : 'admin/queryActivityInfo.do',
		type : "POST",
		dataType : "json",
		data : datas,
		success : function(param) {
			$('#activity_table').datagrid('loadData', param);
		}
	});
}